#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define INITSIZE 100
#define INCRESIZE 10

typedef  int SElemType;
typedef int status;

typedef struct 
{
	SElemType *base,*top;
	int stacksize;
}SqStack;

status InitStack(SqStack &S);
status Push(SqStack &S,SElemType e);
status Pop(SqStack &S,SElemType &e);

status InitStack(SqStack &S){
	S.base = (SElemType*)malloc(INITSIZE*sizeof(SElemType));
	if(!S.base) exit(OVERFLOW);
	S.top=S.base;
	S.stacksize=INITSIZE;
	return OK;
}

status Push(SqStack &S,SElemType e){
	if(S.base-S.top>=S.stacksize) {
		S.base = (SElemType*)realloc(S.base,sizeof(SElemType)*INCRESIZE);
		if(!S.base)exit(OVERFLOW);
		S.stacksize+=INCRESIZE;
	}
	*S.top++=e;
	return OK;
}

status Pop(SqStack &S,SElemType &e){
	if(S.base==S.top)return ERROR;
	e = *--S.top;
	return OK;
}

status StackEmpty(SqStack S){
	if(S.base == S.top)return OK;
	return ERROR;
}

status test(){
	SqStack S;
	int i,e,temp;
	InitStack(S);
	scanf("%d",&i);
	while(i--){
		scanf("%d",&e);
		if(StackEmpty(S))
		{
			Push(S,e);
		}else{
			Pop(S,temp);
			// printf("%d\n",temp );
			if(temp>e)
				temp=e;
			Push(S,temp);
		}
	}
	Pop(S,temp);
	printf("%d\n",temp);
}